using System;
using System.Collections.Generic;
using System.Linq;
using ExcelTemplateEngine.Infrastructure.Logging;

namespace ExcelTemplateEngine.Exceptions
{
    /// <summary>
    /// 异常处理帮助类
    /// </summary>
    public static class ExceptionHelper
    {
        /// <summary>
        /// 包装异常并添加上下文信息
        /// </summary>
        public static TemplateEngineException WrapException(Exception exception, string? context = null, Dictionary<string, object>? additionalContext = null)
        {
            if (exception is TemplateEngineException templateException)
            {
                if (!string.IsNullOrEmpty(context))
                    templateException.AddContext("WrapContext", context!);
                    
                if (additionalContext != null)
                {
                    foreach (var kvp in additionalContext)
                    {
                        templateException.AddContext(kvp.Key, kvp.Value);
                    }
                }
                
                return templateException;
            }
            
            var wrappedException = new GeneralTemplateEngineException(
                "An unexpected error occurred", 
                exception);
                
                            if (!string.IsNullOrEmpty(context))
                    wrappedException.AddContext("WrapContext", context!);
                
            if (additionalContext != null)
            {
                foreach (var kvp in additionalContext)
                {
                    wrappedException.AddContext(kvp.Key, kvp.Value);
                }
            }
            
            return wrappedException;
        }
        
        /// <summary>
        /// 创建模板解析异常
        /// </summary>
        public static TemplateParseException CreateTemplateParseException(string message, string? templatePath = null, string? cellAddress = null, string? templateContent = null)
        {
            return new TemplateParseException(message, templatePath, cellAddress, templateContent);
        }
        
        /// <summary>
        /// 创建数据源异常
        /// </summary>
        public static DataSourceException CreateDataSourceException(string message, string? dataSourceName = null, Exception? innerException = null)
        {
            return new DataSourceException(message, dataSourceName, innerException);
        }
        
        /// <summary>
        /// 创建表达式计算异常
        /// </summary>
        public static ExpressionEvaluationException CreateExpressionException(string message, string? expression = null, string? cellAddress = null, Exception? innerException = null)
        {
            return new ExpressionEvaluationException(message, expression, cellAddress, innerException);
        }
        
        /// <summary>
        /// 创建Excel操作异常
        /// </summary>
        public static ExcelOperationException CreateExcelOperationException(string message, string? operation = null, string? cellAddress = null, Exception? innerException = null)
        {
            return new ExcelOperationException(message, operation, cellAddress, innerException);
        }
        
        /// <summary>
        /// 记录异常日志
        /// </summary>
        public static void LogException(TemplateEngineException exception, ITemplateEngineLogger? logger = null)
        {
            if (logger == null) return;
            
            logger.LogError(exception, "Template engine error: {ErrorCode} - {Message}", 
                exception.ErrorCode, exception.Message);
                
            foreach (var kvp in exception.Context)
            {
                logger.LogDebug("Context: {Key} = {Value}", kvp.Key, kvp.Value);
            }
        }
    }
}